//
//  FeedBackVC.m
//  BigJoy
//
//  Created by Rowling on 2017/7/10.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "FeedBackVC.h"
#import "PhotoCell.h"
#import "PersonalViewModel.h"
@interface FeedBackVC ()<UITextViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UILabel * placeHolderLabel;
@property (weak, nonatomic) IBOutlet UITextView *inputTextView;
@property (weak, nonatomic) IBOutlet UILabel *countLable;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) PersonalViewModel *viewModel;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

@implementation FeedBackVC

-(PersonalViewModel *)viewModel {
    if (_viewModel == nil) {
        _viewModel = [[PersonalViewModel alloc]init];
        [_viewModel bindView:self.view];
        @weakify(self);
        [_viewModel.choseImageSubject subscribeNext:^(id  _Nullable x) {
            @strongify(self);
                [self.baseMutableDataArray addObject:x];
                [self.collectionView reloadData];
        }];
    }
    return _viewModel;
}

-(UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(XMW(75), XMW(75));
        layout.minimumInteritemSpacing = XMW(10);
        layout.sectionInset = UIEdgeInsetsMake(0, XMW(10), 10, XMW(10));
//        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([PhotoCell class]) bundle:nil] forCellWithReuseIdentifier:@"Cell"];
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
}
-(void)creatUI {
    self.placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
    self.placeHolderLabel.font = [UIFont systemFontOfSize:14];
    self.placeHolderLabel.text = self.isShequ ? @"在这编写您此刻的想法~" : @"请填写您的意见吧！";
    self.placeHolderLabel.enabled = NO;
    self.placeHolderLabel.textColor = XMColor(214, 214, 214);
    [self.inputTextView addSubview: self.placeHolderLabel];
    self.inputTextView.delegate = self;
    @weakify(self);
    [[[self.inputTextView rac_textSignal] map:^id _Nullable(NSString * _Nullable value) {
        if (value.length > 150) {
            return [value substringWithRange:NSMakeRange(0, 150)];
        }else {
            return value;
        }
    }] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        NSString * str = x;
        NSInteger count = 150 - str.length;
        NSLog(@"%ld",str.length);
        self.inputTextView.text = x;
        self.countLable.text = [NSString stringWithFormat:@"还可以输入%ld字",count];
    }];
    
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo(self.contentView);
    }];
    
    UIButton * btn = [XMSuperHelper buttonWithTitle:@"提交" Font:[UIFont systemFontOfSize:12] fontColor:XMColor(245, 70, 70)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    btn.hidden = self.isShequ;
    self.bottomView.hidden = !self.isShequ;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];//按回车取消第一相应者
    }
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    self.placeHolderLabel.alpha = 0;//开始编辑时
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{//将要停止编辑(不是第一响应者时)
    if (textView.text.length == 0) {
        self.placeHolderLabel.alpha = 1;
    }
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if ( self.inputTextView.isFirstResponder) {
        [self.inputTextView resignFirstResponder];
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.baseMutableDataArray.count + 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PhotoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    if (indexPath.row == self.baseMutableDataArray.count) {
        cell.headImageView.image = [UIImage imageNamed:@"jiazhaop"];
        cell.closeButton.hidden = YES;
    }else {
        cell.headImageView.image = (UIImage *)self.baseMutableDataArray[indexPath.row];
        cell.closeButton.hidden = NO;
        cell.closeButton.tag = 100 + indexPath.row;
        [cell.closeButton addTarget:self action:@selector(closeButtonCLick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return cell;
}

-(void)closeButtonCLick:(UIButton *)sender {
    NSInteger tag = sender.tag - 100;
    [self.baseMutableDataArray removeObjectAtIndex:tag];
    [self.collectionView reloadData];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.baseMutableDataArray.count) {
        self.viewModel.picCount = 8;
        [self.viewModel showTheChosePhotoView];
    }
}

@end
